﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ERPStore.Extensions;

namespace ERPStore.NewsLetters.MessageReaders
{
	public class NewsLetterTrackMessageReader : Ariane.MessageReaderBase<Events.NewsLetterTrackEvent>
	{
		public NewsLetterTrackMessageReader(
			ERPStore.Logging.ILogger logger
			)
		{
			this.Logger = logger;
		}

		protected ERPStore.Logging.ILogger Logger { get; private set; }

		public override void ProcessMessage(Events.NewsLetterTrackEvent eventMessage)
		{
			Logger.Debug("newsletters process message {0} : {1}", eventMessage.Track.Email, eventMessage.Track.LinkName);

			var parameters = new Dictionary<string, object>();
			parameters.Add("RawUrl", eventMessage.RawUrl);
			parameters.Add("IP", eventMessage.Ip);
			parameters.Add("DestinationUrl", eventMessage.DestinationUrl);
			parameters.Add("TrackingKey", eventMessage.Track);

			CloudSoft.Workflows.ManualWorkflowWorkItem.Run(
				new Workflows.Activities.WriteTrackLog(),
				parameters,
				null,
				(ex) =>
				{
					var logger = ERPStore.GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Logging.ILogger>();
					logger.Error(ex);
				});

		}
	}
}
